﻿
using Framework.Common;
using Framework.Entity;
using Framework.Event;
using Framework.ObjectPool;
using Framework.Scene;
using Framework.Resource;
using Framework.FSM;
using Framework.UI;

namespace Framework
{
    /// <summary>
    /// 框架入口类
    /// </summary>
    public class FrameworkEntry : Singleton<FrameworkEntry>
    {
        /// <summary>
        /// 模块管理器
        /// </summary>
        private ModuleManager m_ModuleManager;
        /// <summary>
        /// 场景管理器
        /// </summary>
        private ISceneManager m_SceneManager;
        /// <summary>
        /// 场景管理器
        /// </summary>
        public ISceneManager SceneManager
        {
            get
            {
                if(m_SceneManager == null)
                {
                    m_SceneManager = m_ModuleManager.GetModule<ISceneManager>();
                    if (m_SceneManager == null)
                        m_SceneManager = m_ModuleManager.AddModule<ISceneManager>();
                }
                return m_SceneManager;
            }
        }
        /// <summary>
        /// 资源管理器
        /// </summary>
        private IResourceManager m_ResourceManager;
        /// <summary>
        /// 资源管理器
        /// </summary>
        public IResourceManager ResourceManager
        {
            get
            {
                if (m_ResourceManager == null)
                {
                    m_ResourceManager = m_ModuleManager.GetModule<IResourceManager>();
                    if (m_ResourceManager == null)
                        m_ResourceManager = m_ModuleManager.AddModule<IResourceManager>();
                }
                return m_ResourceManager;
            }
        }
        /// <summary>
        /// 状态机管理器
        /// </summary>
        private IFsmManager m_FsmManager;
        /// <summary>
        /// 状态机管理器
        /// </summary>
        public IFsmManager FsmManager
        {
            get
            {
                if(m_FsmManager == null)
                {
                    m_FsmManager = m_ModuleManager.GetModule<IFsmManager>();
                    if (m_FsmManager == null)
                        m_FsmManager = m_ModuleManager.AddModule<IFsmManager>();
                }
                return m_FsmManager;
            }
        }
        /// <summary>
        /// 实体管理器
        /// </summary>
        private IEntityManager m_EntityManager;
        /// <summary>
        /// 实体管理器
        /// </summary>
        public IEntityManager EntityManager
        {
            get
            {
                if (m_EntityManager == null)
                {
                    m_EntityManager = m_ModuleManager.GetModule<IEntityManager>();
                    if (m_EntityManager == null)
                        m_EntityManager = m_ModuleManager.AddModule<IEntityManager>();
                }
                return m_EntityManager;
            }
        }
        
        /// <summary>
        /// 事件管理器
        /// </summary>
        private IEventManager m_EventManager;
        /// <summary>
        /// 事件管理器
        /// </summary>
        public IEventManager EventManager
        {
            get
            {
                if (m_EventManager == null)
                {
                    m_EventManager = m_ModuleManager.GetModule<IEventManager>();
                    if (m_EventManager == null)
                        m_EventManager = m_ModuleManager.AddModule<IEventManager>();
                }
                return m_EventManager;
            }
        }
        /// <summary>
        /// 对象池管理器
        /// </summary>
        private IObjectPoolManager m_PoolManager;
        /// <summary>
        /// 对象池管理器
        /// </summary>
        public IObjectPoolManager PoolManager
        {
            get
            {
                if (m_PoolManager == null)
                {
                    m_PoolManager = m_ModuleManager.GetModule<IObjectPoolManager>();
                    if (m_PoolManager == null)
                        m_PoolManager = m_ModuleManager.AddModule<IObjectPoolManager>();
                }
                return m_PoolManager;
            }
        }
      
        /// <summary>
        /// UI管理器
        /// </summary>
        private IUIManager m_UIManager;
        /// <summary>
        /// UI管理器
        /// </summary>
        public IUIManager UIManager
        {
            get
            {
                if(m_UIManager == null)
                {
                    m_UIManager = m_ModuleManager.GetModule<IUIManager>();
                    if (m_UIManager == null)
                        m_UIManager = m_ModuleManager.AddModule<IUIManager>();
                }
                return m_UIManager;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public FrameworkEntry()
        {
            m_ModuleManager = new ModuleManager();
            m_SceneManager = m_ModuleManager.AddModule<ISceneManager>();
           // m_ResourceManager = m_ModuleManager.AddModule<IResourceManager>();
            m_FsmManager = m_ModuleManager.AddModule<IFsmManager>();
            m_EntityManager = m_ModuleManager.AddModule<IEntityManager>();
            m_EventManager = m_ModuleManager.AddModule<IEventManager>();
            m_PoolManager = m_ModuleManager.AddModule<IObjectPoolManager>();
            m_UIManager = m_ModuleManager.AddModule<IUIManager>();
        }
        /// <summary>
        /// 框架轮询接口
        /// </summary>
        /// <param name="frameTime">逻辑一帧时间</param>
        /// <param name="realTime">实际一帧时间</param>
        public void Update(float frameTime, float realTime)
        {
            if (m_ModuleManager != null)
                m_ModuleManager.Update(frameTime, realTime);
        }
        /// <summary>
        /// 框架销毁接口
        /// </summary>
        public void Destroy()
        {
            if (m_ModuleManager != null)
                m_ModuleManager.RemoveAllModule();
            m_ModuleManager = null;
        }
        /// <summary>
        /// 框架复位接口
        /// </summary>
        public void Reset()
        {
            if (m_ModuleManager != null)
                m_ModuleManager.Reset();
        }
    }
}
